<template>
  <div class="mod-calendar">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.title" placeholder="活动名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.categoryId"  clearable filterable placeholder="分类名称"
                   class="width185">
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

<!--      <el-form-item>-->
<!--        <el-select v-model="searchForm.hide"  clearable filterable placeholder="活动隐藏"-->
<!--                   class="width185">-->
<!--          <el-option-->
<!--            v-for="item in hideList"-->
<!--            :key="item.id"-->
<!--            :label="item.title"-->
<!--            :value="item.id">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->


      <el-form-item>
        <el-select v-model="searchForm.status"  clearable filterable placeholder="上架下架"
                   class="width185">
          <el-option
            v-for="item in statusList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

<!--      <el-form-item>-->
<!--        <el-select v-model="searchForm.continuity"  clearable filterable placeholder="活动时间连续"-->
<!--                   class="width185">-->
<!--          <el-option-->
<!--            v-for="item in continuityList"-->
<!--            :key="item.id"-->
<!--            :label="item.title"-->
<!--            :value="item.id">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('event:calendar:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('event:calendar:save')" type="primary" @click="addHandle()">批量导入</el-button>
        <el-button v-if="isAuth('event:calendar:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>


      <el-form-item label="跳转">
        <el-radio-group v-model="searchForm.link" @change="changeLink" >
          <el-radio label="1" >活动日历</el-radio>
          <el-radio label="2">规则富文本</el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>

    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        prop="hide"
        header-align="center"
        align="center"
        label="活动隐藏">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.hide === 1" size="small" type="success">不隐藏</el-tag>
          <el-tag v-else-if="scope.row.hide === 2" size="small" type="danger">隐藏</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="abbreviation"
        header-align="center"
        align="center"
        label="热门活动简称">
      </el-table-column>

      <el-table-column
        prop="sign"
        header-align="center"
        align="center"
        label="活动标识">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sign === 1" size="small" type="success">热门</el-tag>
          <el-tag v-else-if="scope.row.sign === 2" size="small" type="success">推荐</el-tag>

        </template>
      </el-table-column>

      <el-table-column
        prop="sort"
        header-align="center"
        align="center"
        label="排序字段">
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="categoryTitle"
        header-align="center"
        align="center"
        label="卡种">
      </el-table-column>
      <el-table-column
        prop="branchName"
        header-align="center"
        align="center"
        label="支行">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="small" type="success">上架</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="small" type="danger">下架</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        show-tooltip-when-overflow
        prop="link"
        header-align="center"
        align="center"
        label="外链地址">
      </el-table-column>

      <el-table-column
        prop="cover"
        header-align="center"
        align="center"
        label="大图">
        <template slot-scope="scope">
          <img style="height: 50%;width: 50%" @click="openImg(scope.row.cover)" :src="scope.row.cover"/>
        </template>
      </el-table-column>

      <el-table-column
        prop="remarks"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>

      <el-table-column
        prop="introduce"
        header-align="center"
        align="center"
        label="活动日历简介">
      </el-table-column>
      <el-table-column
        show-tooltip-when-overflow
        prop="content"
        header-align="center"
        align="center"
        label="活动日历详情">
      </el-table-column>
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        label="活动开始时间">
      </el-table-column>

      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="活动结束时间">
      </el-table-column>

      <el-table-column
        prop="continuity"
        header-align="center"
        align="center"
        label="活动时间是否连续">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.continuity === 1" size="small" type="success">连续</el-tag>
          <el-tag v-else-if="scope.row.continuity === 2" size="small" type="danger">不连续</el-tag>
        </template>
      </el-table-column>


      <el-table-column
        prop="days"
        header-align="center"
        align="center"
        label="每月">
      </el-table-column>

      <el-table-column
        prop="weeks"
        header-align="center"
        align="center"
        label="每周">
      </el-table-column>




      <el-table-column
        prop="holiday"
        header-align="center"
        align="center"
        label="法定节假日">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.holiday === 1" size="small" type="danger">不包括</el-tag>
          <el-tag v-else-if="scope.row.holiday === 2" size="small" type="success">包括</el-tag>

        </template>
      </el-table-column>

      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="更新时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('event:calendar:info')" type="text" size="small" @click="showDetails(scope.row.id)">
            查看
          </el-button>
          <el-button v-if="isAuth('event:calendar:update')" type="text" size="small"
                     @click="addOrUpdateHandle(scope.row.id)">修改
          </el-button>
          <el-button v-if="isAuth('event:calendar:delete')" type="text" size="small"
                     @click="deleteHandle(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <info-upload v-if="uploadVisible" ref="infoUpload" @refreshDataList="getDataList"></info-upload>
  </div>
</template>

<script>
import AddOrUpdate from './calendar-add-or-update'
import InfoUpload from './calendar-upload'


export default {
  data() {
    return {
      searchForm: {
        title: '',
        categoryId: '',
        link: '',
        status: '',
        hide: '',
        continuity: ''
      },
      uploadVisible:false,
      dataList: [],
      categoryList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      addOrUpdateVisible: false,
      statusList: [{id: 1,title: '上架'},{id: 2, title: '下架'}],
      hideList: [{id: 1,title: '不隐藏'},{id: 2, title: '隐藏'}],
      continuityList: [{id: 1,title: '连续'},{id: 2, title: '不连续'}],
      dataForm: {
        id: 0,
        categoryId: '',
        sort: '',
        hide: '',
        abbreviation: '',
        sign: '',
        title: '',
        status: '',
        link: '',
        cover: '',
        remarks: '',
        content: '',
        startTime: '',
        endTime: '',
        continuity: '',
        days: '',
        weeks: '',
        holiday: '',
        createTime: '',
        updateTime: ''}
    }
  },
  components: {
    AddOrUpdate,InfoUpload
  },
  activated() {
    this.getDataList()
    this.getCategoryList()
  },
  methods: {
    //批量导入
    addHandle(){
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.infoUpload.init()
      })
    },
    // 获取数据列表
    getDataList() {
      this.$http({
        url: '/event/calendar/list',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'title': this.searchForm.title,
          'categoryId': this.searchForm.categoryId,
          'hide': this.searchForm.hide && this.searchForm.hide !== '' ? this.searchForm.hide : null,
          'status': this.searchForm.status && this.searchForm.status !== '' ? this.searchForm.status : null,
          'continuity': this.searchForm.continuity && this.searchForm.continuity !== '' ? this.searchForm.continuity : null
        }
      }).then(({data}) => {
        this.searchForm.link = data.link
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
      if(this.pageIndex!=1&&this.dataList ==null){
          this.pageIndex=1
          this.getDataList();
      }
    },
    // 跳转设置
    changeLink(value){
      console.log(value)
      this.$http({
        url: `/event/calendar/updateLink`,
        method: 'post',
        params: {
          'value': value
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500
          })
        }
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 查看详情
    showDetails(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, true)
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    getCategoryList() {
      this.$http({
        url: `/event/category/queryAll`,
        method: 'get',
        params: {
          'status': 1
        },
        data: this.dataForm
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.categoryList = data.list
        }
      })
    },
    // 删除
    deleteHandle(id) {
      let ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '/event/calendar/delete',
          method: 'post',
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getDataList()
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
